@tool
@icon("../icons/parallel_state.svg")
class_name State_Parallel
extends State

#region 属性
var sub_states:Array[State] = []
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init():
	super()
	for child in get_children():
		if child is State:
			sub_states.append(child)
			child.init()
func enter(expect_transition:bool = false):
	super()
	for state in sub_states:
		state.enter()
func exit():
	for state in sub_states:
		state.exit()
	super()
func step():
	super()
	for state in sub_states:
		state.step()
func handle_transition(transition:Transition,source:State):
	var target = transition.resolve_target()
	if target is not State:
		push_error("Target State 不是一个State类型节点")
		return
	if target == self:
		exit()
		enter(false)
		return
	if self.is_ancestor_of(target):
		for child in get_children():
			if child is State and child.is_ancestor_of(target):
				child.handle_transition(transition,source)
				return
		return
	get_parent().handle_transition(transition,source)
func process_transitions(event:StringName,property_change:bool = false) -> bool:
	if not active:
		return false
	var handled = false
	for state in sub_states:
		var state_handled = state.process_transitions(event,property_change)
		handled = handled or state_handled
	if handled:
		if not property_change:
			self.event_received.emit(event)
		return true
	return super(event,property_change)
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _get_configuration_warnings() -> PackedStringArray:
	var warnings = super._get_configuration_warnings()
	var child_count = 0
	for child in get_children():
		if child is State:
			child_count += 1
	if child_count < 2:
		warnings.append("并行State至少需要俩个子State")
	return warnings
#endregion
